<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtpagecontrol.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class admin_standard_droplist extends Page
{
       public $hfModel = null;
       public $JTModel = null;
       public $JTManufacturer = null;
       public $hfMnfr = null;
       public $btnModelDelete = null;
       public $btnModelEdit = null;
       public $btnModelAdd = null;
       public $btnManufacturerDelete = null;
       public $btnManufacturerEdit = null;
       public $btnManufacturerAdd = null;
       public $editModel = null;
       public $editManufacturer = null;
       public $Label3 = null;
       public $Label2 = null;
       public $btnStandardDelete = null;
       public $hfValueId = null;
       public $editStandardDescription = null;
       public $hfCategory = null;
       public $btnStandardEdit = null;
       public $btnStandardAdd = null;
       public $JTStandardDroplist = null;
       public $hfGetCtr = null;
       public $JTStandardDefaultValues = null;
       public $Label1 = null;
       public $JTPageDroplist = null;
       public $JTSiteTheme1 = null;
       function checkExist($value,$category)
       {
        if($category!='model')
        $query_exist = "SELECT * FROM default_droplist_tbl WHERE value='".$value."' AND category='".$category."'";

        if($category=='model')
        $query_exist = "SELECT * FROM default_model_tbl WHERE mnfr_id='".$this->hfMnfr->getValue()."' AND value='".$value."'";

        $result_exist = mysql_query($query_exist) or die("SELECT EXIST ".mysql_error());
        $exist = mysql_num_rows($result_exist);

        return $exist;
       }

       function admin_standard_droplistJSLoad($sender, $params)
       {

       ?>
       document.getElementById('JTStandardDroplist_colitem_hdr_0').width = '100';
       document.getElementById('JTStandardDroplist_colitem_hdr_1').width = '300';

       document.getElementById('JTStandardDroplist_colitem_body_0').width = '100';
       document.getElementById('JTStandardDroplist_colitem_body_1').width = '300';
       <?php

       }

       function btnModelDeleteClick($sender, $params)
       {
       $model_id = $this->hfModel->getValue();
       $mnfr_id = $this->hfMnfr->getValue();
       if($model_id!=0)
       {
       $query_del_model = "DELETE FROM default_model_tbl WHERE auto_id='".$model_id."'";
       mysql_query($query_del_model) or die("DELETE MODEL ".mysql_error());

       $this->editModel->Text = '';
       }
       redirect("admin_standard_droplist.php?layer=1&mnfr_id=".$mnfr_id);

       }

       function btnModelEditClick($sender, $params)
       {
       $model_id = $this->hfModel->getValue();
       $mnfr_id = $this->hfMnfr->getValue();
       if($model_id!=0)
       {
       if($this->editModel->Text=='' || $this->editModel->Text==null)
       redirect('admin_standard_droplist.php?layer=1');

       $exist = $this->checkExist($this->editModel->Text,'model');
       if($exist==0)
       {
       $query_up_model = "UPDATE default_model_tbl SET value='".$this->editModel->Text."' WHERE auto_id='".$model_id."'";
       mysql_query($query_up_model) or die("UPDATE MODEL ".mysql_error());
       }
       }
       redirect("admin_standard_droplist.php?model_id=".$model_id."&layer=1"."&mnfr_id=".$mnfr_id);
       }

       function btnModelAddClick($sender, $params)
       {
       $mnfr_id = $this->hfMnfr->getValue();
       if($mnfr_id!=0)
       {
       if($this->editModel->Text=='' || $this->editModel->Text==null)
       redirect('admin_standard_droplist.php?layer=1');

       $exist = $this->checkExist($this->editModel->Text,'model');
       if($exist==0)
       {
       $query_ins_model = "INSERT INTO default_model_tbl (mnfr_id,value)
       VALUES ('".$mnfr_id."','".$this->editModel->Text."')";
       mysql_query($query_ins_model) or die("INSERT MODEL ".mysql_error());
       }
       }
       redirect('admin_standard_droplist.php?layer=1&mnfr_id='.$mnfr_id);
       }

       function JTModelShow($sender, $params)
       {
        $mnfr_id = $this->hfMnfr->getValue();
        $model_id = $this->hfModel->getValue();
        $arr_model = array();
        $result_model = mysql_query("SELECT * FROM default_model_tbl WHERE mnfr_id='".$mnfr_id."'") or die("SELECT MODEL".mysql_error());
        while($row_model = mysql_fetch_array($result_model))
        {
          $model_id = $row_model['auto_id'];
          $get_model_id = $this->hfModel->getValue();

          $model_value = $row_model['value'];
          if($model_id==$get_model_id)
          {
          $model_link = "<a style='background-color:yellow' href=admin_standard_droplist.php?layer=1&mnfr_id=".$mnfr_id."&model_id=".$model_id.">".$model_value.
          "</a>";
          }
          if($model_id!=$get_model_id)
          {
          $model_link = "<a href=admin_standard_droplist.php?layer=1&mnfr_id=".$mnfr_id."&model_id=".$model_id.">".$model_value.
          "</a>";
          }
          $arr_model[] = array('Model' => $model_link);
        }
         $this->JTModel->setCellData($arr_model);
       }

       function btnManufacturerDeleteClick($sender, $params)
       {
       $mnfr_id = $this->hfMnfr->getValue();
       if($mnfr_id!=0)
       {
       $query_del_value = "DELETE FROM default_droplist_tbl WHERE auto_id='".$mnfr_id."'";
       mysql_query($query_del_value) or die("DELETE VALUE ".mysql_error());

       $query_del_model = "DELETE FROM default_model_tbl WHERE mnfr_id='".$mnfr_id."'";
       mysql_query($query_del_value) or die("DELETE MODEL ".mysql_error());

       $this->editManufacturer->Text = '';
       }
       redirect("admin_standard_droplist.php?layer=1");
       }

       function btnManufacturerEditClick($sender, $params)
       {
       $mnfr_id = $this->hfMnfr->getValue();
       if($mnfr_id!=0)
       {
       if($this->editManufacturer->Text=='' || $this->editManufacturer->Text==null)
       redirect('admin_standard_droplist.php?layer=1');

       $exist = $this->checkExist($this->editManufacturer->Text,'manufacturer');
       if($exist==0)
       {
       $query_up_value = "UPDATE default_droplist_tbl SET value='".$this->editManufacturer->Text."' WHERE auto_id='".$mnfr_id."'";
       mysql_query($query_up_value) or die("DELETE VALUE ".mysql_error());
       }
       }
       redirect("admin_standard_droplist.php?mnfr_id=".$mnfr_id."&layer=1");

       }

       function btnManufacturerAddClick($sender, $params)
       {
       if($this->editManufacturer->Text=='' || $this->editManufacturer->Text==null)
       redirect('admin_standard_droplist.php?layer=1');

       $exist = $this->checkExist($this->editManufacturer->Text,'manufacturer');
       if($exist==0)
       {
       $query_ins_mnfr = "INSERT INTO default_droplist_tbl (category,value)
       VALUES ('manufacturer','".$this->editManufacturer->Text."')";
       mysql_query($query_ins_mnfr) or die("INSERT MANUFACTURER ".mysql_error());
       }
       redirect('admin_standard_droplist.php?layer=1&mnfr_id=0');
       }

       function JTManufacturerShow($sender, $params)
       {
       $arr_mnfr = array();
       $result_mnfr = mysql_query("SELECT * FROM default_droplist_tbl WHERE category='manufacturer'") or die("SELECT MNFR".mysql_error());
       while($row_mnfr = mysql_fetch_array($result_mnfr))
       {
         $mnfr_id = $row_mnfr['auto_id'];
         $get_mnfr_id = $this->hfMnfr->getValue();
         $mnfr_value = $row_mnfr['value'];
         if($mnfr_id==$get_mnfr_id)
         {
         $mnfr_link = "<a style='background-color:yellow' href=admin_standard_droplist.php?layer=1&mnfr_id=".$mnfr_id.">".$mnfr_value.
         "</a>";
         }
         if($mnfr_id!=$get_mnfr_id)
         {
         $mnfr_link = "<a href=admin_standard_droplist.php?layer=1&mnfr_id=".$mnfr_id.">".$mnfr_value.
         "</a>";
         }
         $arr_mnfr[] = array('Manufacturer' => $mnfr_link);
       }
         $this->JTManufacturer->setCellData($arr_mnfr);


       }

       function btnStandardDeleteClick($sender, $params)
       {
       $value_id = $this->hfValueId->getValue();
       if($value_id!=0)
       {
       $category = $this->hfCategory->getValue();
       $query_del_value = "DELETE FROM default_droplist_tbl WHERE auto_id='".$value_id."'";
       mysql_query($query_del_value);
       $this->editStandardDescription->Text = '';
       redirect("admin_standard_droplist.php?category=".$category);
       }
       }

       function btnStandardEditClick($sender, $params)
       {
       $value_id = $this->hfValueId->getValue();
       if($value_id!=0)
       {
       if($this->editStandardDescription->Text=='' || $this->editStandardDescription->Text==null)
       return false;

       $exist = $this->checkExist($this->editStandardDescription->Text,$this->hfCategory->getValue());
       if($exist==0)
       {
       $category = $this->hfCategory->getValue();
       $query_up_value = "UPDATE default_droplist_tbl SET value='".$this->editStandardDescription->Text."' WHERE auto_id='".$value_id."'";
       mysql_query($query_up_value) or die("UPDATE VALUE ".mysql_error());
       }
       redirect("admin_standard_droplist.php?value_id=".$value_id."&category=".$category);
       }
       }

       function btnStandardAddClick($sender, $params)
       {
       if($this->editStandardDescription->Text=='' || $this->editStandardDescription->Text==null)
       return false;

       $exist = $this->checkExist($this->editStandardDescription->Text,$this->hfCategory->getValue());
       if($exist==0)
       {
       $query_ins_def = "INSERT INTO default_droplist_tbl (category,value) VALUES
       ('".$this->hfCategory->getValue()."','".$this->editStandardDescription->Text."')";
       mysql_query($query_ins_def) or die("INSERT DEFAULT ".mysql_error());
       }
       redirect("admin_standard_droplist.php?value_id=".$value_id."&category=".$category);
       }

       function JTStandardDefaultValuesShow($sender, $params)
       {
       $arr_def = array();
       $query_def = "SELECT * FROM default_droplist_tbl WHERE category='".$this->hfCategory->getValue()."'";
       $result_def = mysql_query($query_def) or die("SELECT DEFAULT ".mysql_error());

       while($row_def = mysql_fetch_array($result_def))
       {
        $value_id = $row_def['auto_id'];
        $get_vid = $this->hfValueId->getValue();
        $category = $this->hfCategory->getValue();
        if($value_id==$get_vid)
        {
        $value = "<a style='background-color:yellow' href='admin_standard_droplist.php?value_id=".$value_id."&category=".$category."'>".$row_def['value']."</a>";
        }
        if($value_id!=$get_vid)
        {
        $value = "<a href='admin_standard_droplist.php?value_id=".$value_id."&category=".$category."'>".$row_def['value']."</a>";
        }
        $arr_def[] = array('Value'=>$value);
       }
       $this->JTStandardDefaultValues->setCellData($arr_def);
       }


       function admin_standard_droplistCreate($sender, $params)
       {
       $get_ctr = $_GET['get_ctr'];
       if(($get_ctr!='' || $get_ctr!=null) && isset($get_ctr))
       $this->hfGetCtr->setValue($get_ctr);

       $get_cat = $_GET['category'];
       if(($get_cat!='' || $get_cat!=null) && isset($get_cat))
       $this->hfCategory->setValue($get_cat);

       $get_vid = $_GET['value_id'];
       if(($get_vid!='' || $get_vid!=null) && isset($get_vid))
       {
        $this->hfValueId->setValue($get_vid);
        $query_val = "SELECT value FROM default_droplist_tbl WHERE auto_id='".$get_vid."'";
        $result_val = mysql_query($query_val) or die("SELECT VALUE ".mysql_error());
        $row_val = mysql_fetch_array($result_val);
        $this->editStandardDescription->Text = $row_val['value'];
       }

       $get_mnfr = $_GET['mnfr_id'];
       if(($get_mnfr!='' || $get_mnfr!=null) && isset($get_mnfr))
       {
       $this->hfMnfr->setValue($get_mnfr);
       $query_mnfr = "SELECT value FROM default_droplist_tbl WHERE auto_id='".$get_mnfr."'";
       $result_mnfr = mysql_query($query_mnfr) or die("SELECT VALUE ".mysql_error());
       $row_mnfr = mysql_fetch_array($result_mnfr);
       $this->editManufacturer->Text = $row_mnfr['value'];
       }

       $get_model = $_GET['model_id'];
       if(($get_model!='' || $get_model!=null) && isset($get_model))
       {
       $this->hfModel->setValue($get_model);
       $query_model = "SELECT value FROM default_model_tbl WHERE auto_id='".$get_model."'";
       $result_model = mysql_query($query_model) or die("SELECT VALUE ".mysql_error());
       $row_model = mysql_fetch_array($result_model);
       $this->editModel->Text = $row_model['value'];
       }

       $this->JTPageDroplist->TabIndex = $_GET['layer'];
       }

       function JTStandardDroplistShow($sender, $params)
       {
       $label_array = array('Fuel', '',
       'Tire', '', '', '',
       'Vehicle', '',
       'Account', 'Expense', '',
       'Inventory', '', '', '',
       'Personnel', '','',
       'Reminder',
       'Work Summary', '','');

       $fields_array = array('Brand', 'Type',
       'Manufacturer', 'Size', 'Type', 'Wheel Size',
       'Incident Type', 'Type/Style',
       'Account Title', 'Description', 'Type',
       'Manufacturer', 'Storeroom/Location', 'Type', 'Unit of Measure',
       'Incident Type', 'Job Title', 'Department',
       'Type',
       'Type', 'Serviced By', 'Labor Description');

       $category_array = array('brands', 'octane/types',
       'tire-manufacturer', 'sizes', 'tire-type', 'wheel sizes',
       'journal categories', 'body styles',
       'accounts', 'expense-descriptions', 'expense-type',
       'inventory-manufacturer', 'storeroom/locations', 'inventory-type', 'unit of measure',
       'driving history types', 'job titles', 'departments',
       'reminder-type',
       'descriptions', 'serviced by', 'task descriptions');

       $tbl_array = array('lookup_fuel_tbl', 'lookup_fuel_tbl',
       'lookup_tire_tbl', 'lookup_tire_tbl', 'lookup_tire_tbl', 'lookup_tire_tbl',
       'lookup_vehicle_tbl', 'lookup_vehicle_tbl',
       'lookup_expense_tbl', 'lookup_expense_tbl', 'lookup_expense_tbl',
       'lookup_inventory_tbl', 'lookup_inventory_tbl', 'lookup_inventory_tbl', 'lookup_inventory_tbl',
       'lookup_personnel_tbl', 'lookup_personnel_tbl', 'lookup_personnel_tbl',
       'lookup_reminder_tbl',
       'lookup_workorder_tbl', 'lookup_workorder_tbl', 'lookup_workorder_tbl');

       $arr = array();
       $ctr = 0;
       $count = count($fields_array);
       while($ctr<=$count)
       {
        if($ctr==$this->hfGetCtr->getValue() && is_numeric($this->hfGetCtr->getValue()))
        {
         $tools = "<a href='admin_standard_droplist.php?category=".$category_array[$ctr]."&get_ctr=".$ctr."&value_id=0' style='background-color:yellow'>".$fields_array[$ctr]."</a>";
        }
        else//if($ctr!=$this->hfGetCtr->getValue())
        {
        $tools = "<a href='admin_standard_droplist.php?category=".$category_array[$ctr]."&get_ctr=".$ctr."&value_id=0'>".$fields_array[$ctr]."</a>";
        }
        $arr[] = array('Drop list Fields ' => $label_array[$ctr],
        'label ' => $tools);
        $ctr++;
       }
       $this->JTStandardDroplist->setCellData($arr);
       }

}

global $application;

global $admin_standard_droplist;

//Creates the form
$admin_standard_droplist=new admin_standard_droplist($application);

//Read from resource file
$admin_standard_droplist->loadResource(__FILE__);

//Shows the form
$admin_standard_droplist->show();

?>